package xyz.chenyongpeng.orderconsumerdubbo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import xyz.chenyongpeng.orderconsumerdubbo.service.ResourceService;
import xyz.chenyongpeng.orderconsumerdubbo.service.UserService;
import xyz.chenyongpeng.orderconsumerdubbo.vo.RelationSeriesVo;
import xyz.chenyongpeng.orderconsumerdubbo.vo.ResourceVo;
import xyz.chenyongpeng.to.GAudioTo;
import xyz.chenyongpeng.to.GoodsTicketTo;
import xyz.chenyongpeng.to.NearbyResourceTo;
import xyz.chenyongpeng.utils.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("Resource")
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private UserService userService;

    /**
     * 取单个资源的详情，r_id 是由智能地图获取到的。
     * @param r_id
     * @param wx_openid
     * @return
     */
    @PostMapping("/get")
    public R get(@RequestParam(name = "r_id",required = true) String r_id,
                 @RequestParam(name = "wx_openid",required = false) String wx_openid){
        if (StringUtils.isEmpty(r_id)){
            return R.error(500, "参数不全");
        }
        ResourceVo resourceVo = resourceService.getOne(r_id);
        if (resourceVo == null){
            return R.error(404, "没有这个资源");
        }

        Map<String,Object> result = new HashMap<>();

        // 轮播图
        List<String> imgs = resourceService.getImgListByLimit(r_id);
        resourceVo.setBanner_img(imgs);

        List<NearbyResourceTo> nearbyResources = resourceService.getNearbyResources(r_id);


        List<GoodsTicketTo> goods_ticket = resourceService.getAllTickets(r_id);

        List<GAudioTo> goods_audio = resourceService.getAllAudio(r_id);


        if (!StringUtils.isEmpty(wx_openid)){
            String u_id = userService.getUserIdByOpenid(wx_openid);
            if (!StringUtils.isEmpty(u_id)){
                goods_audio = resourceService.checkHave(goods_audio,u_id);
            }
        }
        List<RelationSeriesVo> RelationSeriesVo = null;
        if (StringUtils.isEmpty(goods_ticket.get(0).getRelation_series())){
            RelationSeriesVo = resourceService.getRelationSeries(goods_ticket.get(0).getRelation_series());
        }

        List<RelationSeriesVo> RecommendSeriesVo = null;
        if (StringUtils.isEmpty(goods_ticket.get(0).getRecommend_series())){
            RecommendSeriesVo = resourceService.getRecommendSeries(goods_ticket.get(0).getRecommend_series());
        }

        result.put("info", resourceVo);
        result.put("goods_ticket", goods_ticket);
        result.put("relation_series", RelationSeriesVo);
        result.put("recommend_series", RecommendSeriesVo);
        result.put("goods_audio", goods_audio);
        result.put("info_relevant", nearbyResources);
        return R.ok(result);
    }


    /**
     * r_id 是由首页获取到的，也可以写死在程序里面。用于智能地图筛选资源列表。
     * @param rc_id
     * @param level
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestParam(name = "rc_id",required = true) String rc_id,
                 @RequestParam(name = "level",required = false) int level){
        return null;
    }
}
